package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"starry_sky/internal/core"
	"starry_sky/internal/dto/role_dto"
	"starry_sky/internal/model"
	"starry_sky/internal/service"
	"starry_sky/internal/service/role-service"
	"starry_sky/internal/tools/logger"
)

func CreateRole(c *gin.Context) {
	var role model.Role
	err := c.ShouldBindJSON(&role)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	role.Creator = c.GetString("userId")
	err = role_service.CreateRole(&role)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限新增失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(role.ID))
}

func UpdateRole(c *gin.Context) {
	var role model.Role
	err := c.ShouldBindJSON(&role)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	var oldRole model.Role
	oldRole.ID = role.ID
	err = service.FindById(&oldRole)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限更新失败"))
		return
	}
	oldRole.Name = role.Name
	oldRole.Code = role.Code
	oldRole.Sort = role.Sort
	oldRole.Remark = role.Remark
	oldRole.Updater = c.GetString("userId")
	err = role_service.UpdateRole(&oldRole)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限更新失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func UpdateRoleStatus(c *gin.Context) {
	var roleUpdateStatus role_dto.RoleUpdateStatus
	err := c.ShouldBindJSON(&roleUpdateStatus)
	if logger.HasError(err) && roleUpdateStatus.ID == "" {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	var role model.Role
	role.ID = roleUpdateStatus.ID
	updateMap := make(map[string]any)
	updateMap["status"] = roleUpdateStatus.Status
	updateMap["updater"] = c.GetString("userId")
	err = service.UpdateForMap(&role, updateMap)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限更新失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func DeleteRole(c *gin.Context) {
	var role model.Role
	err := c.ShouldBindJSON(&role)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.Delete(&role)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限删除失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func FindRoleById(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	var role model.Role
	role.ID = id
	err := service.FindById(&role)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(role))
}

func PageRole(c *gin.Context) {
	support := core.PageSupport[model.Role]{}
	err := c.ShouldBind(&support)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindPage(&support, map[string]any{}, make([]string, 0), "name")
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(support))
}

// BindRoleAndPermission 绑定角色和权限
func BindRoleAndPermission(c *gin.Context) {
	var bindRoleAndPermission role_dto.BindRoleAndPermission
	err := c.ShouldBindJSON(&bindRoleAndPermission)
	if logger.HasError(err) || bindRoleAndPermission.RoleId == "" || len(bindRoleAndPermission.MenuIds) == 0 {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = role_service.BindRoleAndPermission(&bindRoleAndPermission)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "绑定角色权限失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func AllRoleList(c *gin.Context) {
	var roles []model.Role
	err := service.FindAll(&roles)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(roles))
}
